﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityStudentManagemnetSystem.BLL;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.UI
{
    public partial class StudentEnrollCourses : System.Web.UI.Page
    {
        private CourseEnrollManager manager = new CourseEnrollManager();
        private DateTime enrollDate = DateTime.Today;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Account/Login.aspx", false);
            }
            if (IsPostBack != true)
            {
                dateTextBox.Text = enrollDate.ToString();
            }
            PopulatedGridView();

        }

        protected void studentEnrollButton_Click(object sender, EventArgs e)
        {
            try
            {
                string message;
                Student aStudent = new Student();
                aStudent.RegNo = regNoTextBox.Text;
                aStudent.Name = nameTextBox.Text;
                aStudent.Email = emailTextBox.Text;
                aStudent.CourseEnrollDate = Convert.ToDateTime(dateTextBox.Text);
                aStudent.Department.DeptId = Convert.ToInt32(departmentIdLabel.Text);
                aStudent.Course.CourseId = Convert.ToInt32(courseEnrollDropDownList1.SelectedValue);
                aStudent.Course.Code = courseEnrollDropDownList1.SelectedItem.ToString();
                string save = manager.SaveStudentInformation(aStudent, out message);
                if (save != null)
                {
                    messagelabel.ForeColor = Color.Magenta;
                    messagelabel.Text = message;
                    PopulatedGridView();
                }
            }
                catch(FormatException exception)
                {
                    messagelabel.ForeColor = Color.Red;
                    messagelabel.Text = exception.Message;
                }
            catch (Exception exception)
            {

                messagelabel.ForeColor = Color.Red;
                messagelabel.Text = exception.Message;
            }
          

        }

        private void PopulatedGridView()
        {
            string message1;
            string studentRegNo = regNoTextBox.Text;
            DataTable courseTable = manager.GetAllEnrollCourses(studentRegNo, out message1);
            enrollCourseMessageLabel.ForeColor = Color.BlueViolet;
            enrollCourseMessageLabel.Text = message1;
            enrollCoursesGridView.DataSource = courseTable;
            enrollCoursesGridView.DataBind();
        }

        protected void studentSearchButton_Click(object sender, EventArgs e)
        {

            string regno = regNoTextBox.Text;
            Student aStudent = manager.SearchStudentByRegNo(regno);
            nameTextBox.Text = aStudent.Name;
            emailTextBox.Text = aStudent.Email;
            departmentNameTextBox.Text = aStudent.Department.Name;
            departmentIdLabel.Text = aStudent.Department.DeptId.ToString();
            PopulatedCourseDropDownList();
            if (aStudent != null)
            {
                PopulatedGridView();
            }

        }

        private void PopulatedCourseDropDownList()
        {
            string departmentId = departmentIdLabel.Text;
            DataTable coursesTable = manager.GetAllCourseForSelectedDepartment(departmentId);
            courseEnrollDropDownList1.DataTextField = "Code";
            courseEnrollDropDownList1.DataValueField = "Id";
            courseEnrollDropDownList1.DataSource = coursesTable;
            courseEnrollDropDownList1.DataBind();
        }
    }
}